package cn.pengpeng.day04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * 单机版的词频统计
 * @author root
 *
 */
public class SingleWC {
	public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
		Map<String, Integer> map = new HashMap<>();
		
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "root");
		
		FSDataInputStream inputStream = fs.open(new Path("/wc.txt"));
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		String line = null;
		while((line = reader.readLine())!=null){
			//System.out.println(line);
			String[] split = line.split(" ");
			for (String word : split) {
				Integer count = map.getOrDefault(word, 0);
				count++;
				map.put(word, count);
			}
			
		}
		FSDataOutputStream create = fs.create(new Path("/part-r-000001"));
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			create.write((entry.getKey() + "=" +entry.getValue()+"\r\n").getBytes());
			//System.out.println(entry);
		}
		create.close();
		reader.close();
		fs.close();
		System.out.println("结束任务");
	}

}
